﻿// See https://aka.ms/new-console-template for more information
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using Com.Client.Common;
using Com.Client.Meeting;
using ProtoPack;

static void AssertEqual<T>(T v1, T v2, string msg)
{
    if (!object.Equals(v1, v2))
    {
        Console.WriteLine($"字段：{msg}，值：{v1}与期望值{v2}不同");
        Environment.Exit(0);
    }
}

static void AssertArrayEqual<T>(List<T> v1, List<T> v2, string msg)
{
    if (v1.Count() != v2.Count())
    {
        Console.WriteLine($"字段：{msg}，值与期望值长度不同");
        Environment.Exit(0);
    }

    for (int i = 0; i < v1.Count(); i++)
    {
        if (!object.Equals(v1[i], v2[i]))
        {
            Console.WriteLine($"字段：{msg}，值：{v1[i]}与期望值{v2[i]}不同");
            Environment.Exit(0);
        }
    }
}

MeetingData md = new();

MeetingInfo mi = new()
{
    Chatters = [17, 89],
    Listeners = ["xxyy", "jidess", "🇨🇳对其jiiu====☀️"],
    ReduceRate = 12456.789,
    Price = -90.789F,
    MeetingState = (byte)MeetingState.MEETING_STATE_CANCEL,
    MeetingType = MeetingType.MEETING_TYPE_AUDIO
};

MeetingInfo mi2 = new()
{
    Chatters = [27, 99],
    Listeners = ["ppooiii", "yxbbdg", "中国在这里等了你!"],
    ReduceRate = -42.758,
    Price = -93764.8903F,
    MeetingState = (byte)MeetingState.MEETING_STATE_END,
    MeetingType = MeetingType.MEETING_TYPE_VIDEO
};

KeyValue kv = new()
{
    Key = "test",
    Value = "value"
};

md.MeetingList = new()
{
    MeetingInfos = [mi, mi2],
    MemberInfos = [kv],
    UserKey = new()
    {
        Key = "ttt!",
        Value = "ppp!"
    }
};

ByteArray buf = new();

md.Encode(buf, 0xFFFFFFFFFFFFFFFF);

byte[] mdMarshalData = {
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xee, 0x01, 0x0a, 0xeb, 0x01, 0x01,
	0x65, 0x01, 0x03, 0x78, 0x79, 0x7a, 0x02, 0x01, 0x01, 0x03, 0x01, 0x03, 0x04, 0x08, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11,
	0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x59, 0x06, 0x04, 0x78, 0x78, 0x79, 0x79,
	0x06, 0x06, 0x6a, 0x69, 0x64, 0x65, 0x73, 0x73, 0x06, 0x1c, 0xf0, 0x9f, 0x87, 0xa8, 0xf0, 0x9f,
	0x87, 0xb3, 0xe5, 0xaf, 0xb9, 0xe5, 0x85, 0xb6, 0x6a, 0x69, 0x69, 0x75, 0x3d, 0x3d, 0x3d, 0x3d,
	0xe2, 0x98, 0x80, 0xef, 0xb8, 0x8f, 0x07, 0x08, 0x40, 0xc8, 0x54, 0x64, 0xfd, 0xf3, 0xb6, 0x46,
	0x08, 0x04, 0xc2, 0xb5, 0x93, 0xf8, 0x01, 0x65, 0x01, 0x03, 0x78, 0x79, 0x7a, 0x02, 0x01, 0x02,
	0x03, 0x01, 0x02, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x05, 0x08, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x63, 0x06, 0x07, 0x70, 0x70, 0x6f, 0x6f, 0x69, 0x69, 0x69, 0x06, 0x06, 0x79, 0x78, 0x62, 0x62,
	0x64, 0x67, 0x06, 0x19, 0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0xe5, 0x9c, 0xa8, 0xe8, 0xbf, 0x99,
	0xe9, 0x87, 0x8c, 0xe7, 0xad, 0x89, 0xe4, 0xba, 0x86, 0xe4, 0xbd, 0xa0, 0x21, 0x07, 0x08, 0xc0,
	0x45, 0x61, 0x06, 0x24, 0xdd, 0x2f, 0x1b, 0x08, 0x04, 0xc7, 0xb7, 0x22, 0x72, 0x02, 0x0c, 0x01,
	0x04, 0x74, 0x74, 0x74, 0x21, 0x02, 0x04, 0x70, 0x70, 0x70, 0x21, 0x03, 0x0d, 0x01, 0x04, 0x74,
	0x65, 0x73, 0x74, 0x02, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65,
};

if (mdMarshalData.Length != buf.Length())
{
    Console.WriteLine("md编码失败，编码长度不对");
    Console.WriteLine(BitConverter.ToString(buf.GetBytes()));
    Environment.Exit(0);
}

var encodeData = buf.GetBytes();
for (int i = 0; i < mdMarshalData.Length; i++)
{
    if (encodeData[i] != mdMarshalData[i])
    {
        Console.WriteLine("md编码失败，数据不对");
        Console.WriteLine(BitConverter.ToString(buf.GetBytes()));
        Environment.Exit(0);
    }
}

MeetingData md2 = new();
int iRet = md2.Decode(buf.GetAllocBytes(), 0, (uint)buf.Length());

if (iRet != 0)
{
    Console.WriteLine("md解码失败，返回错误");
    Environment.Exit(0);
}

if (md.ToJson() != md2.ToJson())
{
    Console.WriteLine("md解码失败，解码数据不对");
    Environment.Exit(0);
}

AssertEqual(md2.MeetingList?.MeetingInfos.Count(), 2, "MeetingInfos长度");
AssertEqual(md2.MeetingList?.MeetingInfos[0].MeetingID, "xyz", "MeetingID");
AssertEqual(md2.MeetingList?.MeetingInfos[0].MeetingType, MeetingType.MEETING_TYPE_AUDIO, "MeetingType");
AssertEqual(md2.MeetingList?.MeetingInfos[0].MeetingState, (byte)MeetingState.MEETING_STATE_CANCEL, "MeetingState");
AssertEqual(md2.MeetingList?.MeetingInfos[0].BeginTM, CommonConstants.TEST_UINT16, "BeginTM");
AssertArrayEqual<ulong>(md2.MeetingList.MeetingInfos[0].Chatters, [17, 89], "Chatters");
AssertArrayEqual<string>(md2.MeetingList.MeetingInfos[0].Listeners, ["xxyy", "jidess", "🇨🇳对其jiiu====☀️"], "Listeners");
AssertEqual(md2.MeetingList?.MeetingInfos[0].ReduceRate, 12456.789, "ReduceRate");
AssertEqual(md2.MeetingList?.MeetingInfos[0].Price, (float)-90.789, "Price");

AssertEqual(md2.MeetingList.MeetingInfos[1].MeetingID, "xyz", "MeetingID");
AssertEqual(md2.MeetingList.MeetingInfos[1].MeetingType, MeetingType.MEETING_TYPE_VIDEO, "MeetingType");
AssertEqual(md2.MeetingList.MeetingInfos[1].MeetingState, (byte)MeetingState.MEETING_STATE_END, "MeetingState");
AssertEqual(md2.MeetingList.MeetingInfos[1].BeginTM, CommonConstants.TEST_UINT16, "BeginTM");
AssertArrayEqual<ulong>(md2.MeetingList.MeetingInfos[1].Chatters, [27, 99], "Chatters");
AssertArrayEqual<string>(md2.MeetingList.MeetingInfos[1].Listeners, ["ppooiii", "yxbbdg", "中国在这里等了你!"], "Listeners");
AssertEqual(md2.MeetingList.MeetingInfos[1].ReduceRate, -42.758, "ReduceRate");
AssertEqual(md2.MeetingList.MeetingInfos[1].Price, (float)-93764.8903, "Price");

AssertEqual(md2.MeetingList?.MemberInfos.Count(), 1, "MemberInfos长度");
AssertEqual(md2.MeetingList?.MemberInfos[0].Key, "test", "MemberInfos test");
AssertEqual(md2.MeetingList?.MemberInfos[0].Value, "value", "MemberInfos value");

AssertEqual(md2.MeetingList?.UserKey?.Key, "ttt!", "UserKey key");
AssertEqual(md2.MeetingList?.UserKey?.Value, "ppp!", "UserKey value");

Console.WriteLine("md编码、解码验证成功");
